# -*- coding: utf-8 -*-
# @Time    : 2024/9/11 下午8:57
# @Author  : liujun
# @Email   : 2592730854@qq.com
# @File    : log.py
# @Software: PyCharm

"""
配置log 输出格式time-loglevel-file-func-line-msg
支持输出到log文件及屏幕
支持返回一个logger
"""
import time
import logging
import os
from common.consts import BASE_PATH as base_path
from config.config import Config


class Log:
    @classmethod
    def _config(cls):
        c = Config()
        log_dir = os.path.join(base_path,c.get_runtime("log_dir"))
        now = time.strftime("%Y-%m-%d",time.localtime(time.time()))
        log_file = os.path.join(log_dir,now + ".log")

        # 获取一个标准的logger，配置loglevel
        cls.logger = logging.getLogger()
        cls.logger.setLevel (eval("logging. "+ c. get_runtime("log_level"). upper()))

        # 建立不同handler
        file_handler = logging. FileHandler(log_file,mode="a",encoding='utf-8')
        console_handler = logging.StreamHandler()

        #定义输出格式
        format = logging.Formatter("%(asctime)s-%(filename)s[line:%(lineno)d]-%(levelname)s:%(message)s")
        file_handler.setFormatter(format)
        console_handler.setFormatter(format)

        #把定制handler 添加到我们 logger
        cls.logger.addHandler(file_handler)
        cls.logger.addHandler(console_handler)

    @classmethod
    def get_logger(cls):
        cls._config()
        return cls.logger


if __name__ == '__main__':
    logger=Log.get_logger()
    logger.info("info消息...........")
    logger.error("错误的啦.......")
    logger.debug("bug.......")
